/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.edu.hfut.dmic.webcollector.ui;

import java.io.File;
import cn.edu.hfut.dmic.webcollector.crawler.BasicCrawler;

/**
 *
 * @author hu
 */
public class BasicCrawlerUI extends javax.swing.JFrame {

    /**
     * Creates new form BasicCrawlerUI
     */
    BasicCrawler crawler = new BasicCrawler();
    int threads=10;
    public BasicCrawlerUI() {
        initComponents();
        File f=new File("download");
        txt_path.setText(f.getAbsolutePath());
        
        txt_threads.setText(threads+"");
        String help="至少添加一个种子（种子即爬虫的起始页面的网址，如果要爬去整个站点，可以用站点主页的网址作为种子)\n"
                +"例如爬取新华网，就也可以添加一个种子： http://www.xinhuanet.com/\n"
                +"正则起到约束爬取页面的URL的作用，如果不添加正则，默认爬取种子所在域名下的所有网页和文件\n"
                +"本地路径制定爬取页面在本地的存放路径\n";
        
       output(help);
        //crawler.addRegex("http://.*xinhuanet.com/.*");
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txt_seed = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btn_addseed = new javax.swing.JButton();
        txt_pregex = new javax.swing.JTextField();
        btn_addpregex = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        txt_output = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        txt_path = new javax.swing.JTextField();
        btn_start = new javax.swing.JButton();
        btn_stop = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txt_threads = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txt_nregex = new javax.swing.JTextField();
        btn_addnregex = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("基础爬虫");

        jLabel1.setText("种子:");

        jLabel2.setText("允许的正则(可选):");

        btn_addseed.setText("添加");
        btn_addseed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_addseedActionPerformed(evt);
            }
        });

        btn_addpregex.setText("添加");
        btn_addpregex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_addpregexActionPerformed(evt);
            }
        });

        txt_output.setColumns(20);
        txt_output.setRows(5);
        jScrollPane2.setViewportView(txt_output);

        jLabel3.setText("本地路径:");

        txt_path.setText("download");
        txt_path.setToolTipText("");

        btn_start.setText("开始");
        btn_start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_startActionPerformed(evt);
            }
        });

        btn_stop.setText("停止");
        btn_stop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_stopActionPerformed(evt);
            }
        });

        jLabel4.setText("线程数:");

        jLabel5.setText("禁止的正则(可选)：");
        jLabel5.setToolTipText("");

        btn_addnregex.setText("添加");
        btn_addnregex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_addnregexActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_start)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_stop))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 676, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txt_nregex, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txt_threads, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txt_pregex, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
                                .addComponent(txt_seed, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txt_path, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(35, 35, 35)
                                    .addComponent(btn_addseed))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btn_addpregex)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(btn_addnregex)))))
                .addContainerGap(101, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_seed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_addseed)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_pregex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_addpregex))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_nregex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_addnregex)
                    .addComponent(jLabel5))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txt_path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txt_threads, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_start)
                    .addComponent(btn_stop))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel2.getAccessibleContext().setAccessibleName("允许的正则(可选)：");
        jLabel5.getAccessibleContext().setAccessibleName("禁止的正则（可选）:");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_addseedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_addseedActionPerformed
        // TODO add your handling code here:
        String seed=txt_seed.getText();
        crawler.addSeed(seed);
        output("添加种子:"+seed);
    }//GEN-LAST:event_btn_addseedActionPerformed

    private void btn_startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_startActionPerformed
        // TODO add your handling code here:
        if(!crawler.couldStart()){
            output("请添加至少一个种子URL!");
            return;
        }
        String localpath=txt_path.getText().trim();
        crawler.setRoot(localpath);
        try{
        threads=Integer.valueOf(txt_threads.getText());
        }catch(Exception e){
            
        }
        crawler.start(threads);
        output("开始爬取，请前往"+localpath+"文件夹查看爬取文件,不要关闭此窗口！");
    }//GEN-LAST:event_btn_startActionPerformed

    private void btn_addpregexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_addpregexActionPerformed
        // TODO add your handling code here:
        String regex=txt_pregex.getText();
        crawler.addRegex("+"+regex);
        output("添加正例正则:"+regex);
    }//GEN-LAST:event_btn_addpregexActionPerformed

    
    private void output(String line){
        txt_output.append(line+"\n");
    }
    
    private void btn_stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_stopActionPerformed
        // TODO add your handling code here:
        crawler.stop();
        output("停止");
        
    }//GEN-LAST:event_btn_stopActionPerformed

    private void btn_addnregexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_addnregexActionPerformed
        // TODO add your handling code here:
        String regex=txt_pregex.getText();
        crawler.addRegex("-"+regex);
        output("添加反例正则:"+regex);
    }//GEN-LAST:event_btn_addnregexActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BasicCrawlerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BasicCrawlerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BasicCrawlerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BasicCrawlerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BasicCrawlerUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_addnregex;
    private javax.swing.JButton btn_addpregex;
    private javax.swing.JButton btn_addseed;
    private javax.swing.JButton btn_start;
    private javax.swing.JButton btn_stop;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField txt_nregex;
    private javax.swing.JTextArea txt_output;
    private javax.swing.JTextField txt_path;
    private javax.swing.JTextField txt_pregex;
    private javax.swing.JTextField txt_seed;
    private javax.swing.JTextField txt_threads;
    // End of variables declaration//GEN-END:variables
}
